package com.sfh.dog.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.util.FaceUtil;

public class FaceDalig extends PopupWindow
{
	private GridView mFaceGV;

	public interface IFace
	{
		public void delFace();
		public void selectFace(SpannableString spannableString);
		public void dismiss();
	}

	private IFace mFace;

	public FaceDalig(final Context context, IFace face)
	{
		super(context);
		this.mFace = face;
		View view = LayoutInflater.from(context).inflate(R.layout.face_dialog,
				null);
		mFaceGV = (GridView) view.findViewById(R.id.gvFace);
		view.findViewById(R.id.ivDel).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (mFace != null)
				{
					mFace.delFace();
				}
			}
		});

		view.findViewById(R.id.ivFace).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});

		setContentView(view);
		view.setFocusable(true);
		this.setFocusable(true);
		this.setWidth(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		this.setHeight(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
		colorDrawable.setAlpha(0);
		this.setBackgroundDrawable(colorDrawable);
		mFaceGV.setAdapter(new FaceAdapter(context));
		mFaceGV.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (mFace != null)
				{
					mFace.selectFace(FaceUtil.selectFace(context,
							DogApplication.heads[arg2]));
				}
			}
		});
	}

	@Override
	public void dismiss()
	{
		super.dismiss();
		if (mFace != null)
		{
			mFace.dismiss();
		}
	}

	public static class FaceAdapter extends BaseAdapter
	{

		private LayoutInflater inflater;
		private int[] heads;

		public FaceAdapter(Context context)
		{
			this.inflater = LayoutInflater.from(context);
			this.heads = DogApplication.heads;
		}

		@Override
		public int getCount()
		{
			return heads.length;
		}

		@Override
		public Object getItem(int position)
		{
			return heads[position];
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder viewHolder;
			if (convertView == null)
			{
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.face, parent, false);
				viewHolder.faceIV = (ImageView) convertView
						.findViewById(R.id.face_iv);
				convertView.setTag(viewHolder);
			} else
			{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.faceIV.setImageResource(heads[position]);
			return convertView;
		}

		static class ViewHolder
		{
			ImageView faceIV;
		}
	}
}
